//
//  ISDUETNewsDetailViewController.m
//  12Cung
//
//  Created by Tran Bao Thai on 8/8/12.
//  Copyright (c) 2012 Tran Bao Thai. All rights reserved.
//

#import "ISDUETNewsDetailViewController.h"
#import "Constants.h"
//#import "MKStoreManager.h"

@interface ISDUETNewsDetailViewController (){
    BOOL loadContendSuccessful;
}

- (void) loadNewsDetail;
- (void) showWebView;

@end

@implementation ISDUETNewsDetailViewController

@synthesize typeOfNew;
@synthesize newsItem;
@synthesize lblNaviTitle;
@synthesize newsImageView;
@synthesize lblTitle;
@synthesize lblIntroduce;
@synthesize detailWebView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        loadContendSuccessful = YES;
    }
    return self;
}

- (void) viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.hidden = YES;
    [detailWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:newsItem.detailLink]]];
    loadContendSuccessful = YES;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.hidden = YES;
    if (typeOfNew == HD_TINTUC) {
        lblNaviTitle.text = @"Tin tức";
    } else if(typeOfNew == HD_TVHN){
        lblNaviTitle.text = @"Tử vi hàng ngày";
    }
    detailWebView.hidden = YES;
    newsImageView.image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:newsItem.image]]];
    lblTitle.text = newsItem.title;
    lblIntroduce.text = newsItem.introduce;
    
    googleBannerView = [[GADBannerView alloc] initWithFrame:CGRectMake(0, self.view.frame.size.height - GAD_SIZE_320x50.height, GAD_SIZE_320x50.width, GAD_SIZE_320x50.height)];
    googleBannerView.adUnitID = @"a14f183b45ada4f";
    googleBannerView.rootViewController = self;
    [self.view addSubview:googleBannerView];
    [googleBannerView loadRequest:[GADRequest request]];
}

- (void)viewDidUnload
{
    [self setNewsImageView:nil];
    [self setLblTitle:nil];
    [self setLblIntroduce:nil];
    [self setDetailWebView:nil];
    [self setLblNaviTitle:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)dealloc {
    [typeOfNew release];
    [newsItem release];
    [newsImageView release];
    [lblTitle release];
    [lblIntroduce release];
    [detailWebView release];
    [lblNaviTitle release];
    [super dealloc];
}

- (void) loadNewsDetail{
    progressHUD = [[[MBProgressHUD alloc]initWithView:self.navigationController.view] autorelease];
    [self.navigationController.view addSubview:progressHUD];
    progressHUD.delegate = self;
    progressHUD.labelText = @"Loading...";
    [progressHUD showWhileExecuting:@selector(showWebView) onTarget:self withObject:nil animated:YES];
}
- (void) showWebView{
    detailWebView.hidden = NO;
    sleep(1);
}

- (IBAction)backAction:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)detailAction:(UIButton *)sender {
    [self loadNewsDetail];
}

// Webview delegate
- (void) webViewDidStartLoad:(UIWebView *)webView{
    NSLog(@"Webview start loading");
}

- (void) webViewDidFinishLoad:(UIWebView *)webView{
    NSLog(@"Webview did finished");
    loadContendSuccessful = YES;
}

- (void) webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error{
    loadContendSuccessful = NO;
    NSLog(@"Error when loading web content");
    UIAlertView* errAlert = [[[UIAlertView alloc]initWithTitle:@"Loading Fail" message:@"Please check your Internet connection" delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles: nil]autorelease];
    [errAlert show];
}

// MBProgressHUD
- (void)hudWasHidden{
    /*
    if (loadContendSuccessful) {
        detailWebView.hidden = NO;
    } else {
        detailWebView.hidden = YES;
    }*/
}

@end
